package presale_house.show.view;

import presale_house.entity.ClientDo;
import presale_house.entity.HouseDo;
import presale_house.handler.AddClientViewHandler;
import presale_house.show.MainView;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class AddClientView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel nameLabel=new JLabel("姓名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel sexLabel=new JLabel("性别",JLabel.RIGHT);
//    JTextField sexText=new JTextField();
    JComboBox sex;
    JLabel phoneLabel=new JLabel("电话",JLabel.RIGHT);
    JTextField phoneText=new JTextField();
    JLabel addressLabel=new JLabel("地址",JLabel.RIGHT);
    JTextField addressText=new JTextField();

    JLabel ideal_houseLabel=new JLabel("理想户型",JLabel.RIGHT);
    //JTextField ideal_houseText=new JTextField();
    JComboBox ideal_house;

    JLabel select_numLabel=new JLabel("预售选号",JLabel.RIGHT);
    JTextField select_numText=new JTextField();
    JButton addBtn=new JButton("添加");

    HouseDo houseDo =new HouseDo();
    AddClientViewHandler addClientViewHandler;



    public AddClientView(MainView mainView){
        super(mainView,"添加客户",true);
        addClientViewHandler =new AddClientViewHandler(this,mainView);
        //添加组件
        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        Vector<String>sexData=new Vector<>();
        sexData.addElement("男");
        sexData.addElement("女");
        sex=new JComboBox(sexData);
        sexLabel.setPreferredSize(new Dimension(80,30));
        sex.setPreferredSize(new Dimension(200,30));
        jPanel.add(sexLabel);
        jPanel.add(sex);

        phoneLabel.setPreferredSize(new Dimension(80,30));
        phoneText.setPreferredSize(new Dimension(200,30));
        jPanel.add(phoneLabel);
        jPanel.add(phoneText);
        addressLabel.setPreferredSize(new Dimension(80,30));
        addressText.setPreferredSize(new Dimension(200,30));
        jPanel.add(addressLabel);
        jPanel.add(addressText);

        ideal_houseLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(ideal_houseLabel);
        Vector<String>data= houseDo.getHouseLayouts();
        ideal_house=new JComboBox(data);
        ideal_house.setPreferredSize(new Dimension(200,30));
        jPanel.add(ideal_house);




        select_numLabel.setPreferredSize(new Dimension(80,30));
        select_numText.setPreferredSize(new Dimension(200,30));
        jPanel.add(select_numLabel);
        jPanel.add(select_numText);



        addBtn.addActionListener(addClientViewHandler);
        jPanel.add(addBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    //设置一个客户信息
    public ClientDo buildClient(){
        ClientDo clientDo =new ClientDo();
        clientDo.setName(nameText.getText());
        clientDo.setSex(sex.getSelectedItem().toString());
        clientDo.setPhone(phoneText.getText());
        clientDo.setAddress(addressText.getText());
     //   clientDo.setIdea_house(ideal_houseText.getText());   //!!!注意 JTextfield获取数据,需要用包装类转换
        clientDo.setIdea_house(ideal_house.getSelectedItem().toString());
        clientDo.setSelect_num(select_numText.getText());
        return clientDo;
    }
}
